<?php


/**
 * 同步积分
 * @param string $fromApp	来自应用(即当前应用),应用的名称由函数内的$database的key决定
 * @param string $toApp	目标应用
 * @param string $username	用户名称
 * @param string $email	用户Email
 */
function integralSynchronous($fromApp, $toApp,$username,$email) {

	/* 积分兑换比例均按1:1处理 */
	/**
	 * 积分兑换规则
	 *
	 * @var array
	 */
	$pointExchangeRules = array ();

	$database = array (
			'hstyle' => array (
					'db_host' => '192.168.1.152:3306',
					'db_user' => 'hstyle',
					'db_password' => 'HstyleHE!@#_hhdyse',
					'db_name' => 'hdys_web',
					'db_prefix' => 'ecs_',
					'db_charset' => 'utf8',
					'db_table' => 'users',
					'db_field_point' => 'pay_points',
					'db_field_username' => 'user_name',
					'db_field_email' => 'email'
			),
			'share' => array (
					'db_host' => '192.168.1.152:3306',
					'db_user' => 'hstyle',
					'db_password' => 'HstyleHE!@#_hhdyse',
					'db_name' => 'share',
					'db_prefix' => 'fanwe_',
					'db_charset' => 'utf8',
					'db_table' => 'user',
					'db_field_point' => 'credits',
					'db_field_username' => 'user_name',
					'db_field_email' => 'email'
			)
	);


	$dbFrom = $database [$fromApp];
	$dbTo = $database [$toApp];

	$pointFrom = 0; // 当前应用积分
	$pointTo = 0; // 目标应用积分

	/* 定义双方应用的表名 */
	$tableNameFrom = '`'.$dbFrom ['db_name'].'`.'.$dbFrom ['db_prefix'] . $dbFrom ['db_table'];
	$tableNameTo = '`'.$dbTo ['db_name'].'`.'.$dbTo ['db_prefix'] . $dbTo ['db_table'];

	/* 获取当前应用的积分 */
	$linkFrom = mysql_connect ( $dbFrom ['db_host'], $dbFrom ['db_user'], $dbFrom ['db_password'] );
	if (!$linkFrom) {
		return "Could not connect: " . mysql_error ($linkFrom);
	}

	mysql_select_db ( $dbFrom ['db_name'], $linkFrom );
	mysql_set_charset ( $dbFrom ['db_charset'], $linkFrom );
	$sql = "SELECT {$dbFrom['db_field_point']} point FROM {$tableNameFrom} WHERE {$dbFrom['db_field_username']}='{$username}' AND {$dbFrom['db_field_email']}='{$email}' ";
	$resultFrom = mysql_query ( $sql, $linkFrom );
	while ( $row = mysql_fetch_array ( $resultFrom ) ) {
		$pointFrom = $row ['point'];
	}
	mysql_free_result ( $resultFrom );

	/* 连接目标数据库 */
	$linkTo = mysql_connect ( $dbTo ['db_host'], $dbTo ['db_user'], $dbTo ['db_password'] ) or die ( "Could not connect: " . mysql_error () );
	if (!$linkTo) {
		return "Could not connect: " . mysql_error ($linkTo);
	}
	mysql_select_db ( $dbTo ['db_name'], $linkTo );
	mysql_set_charset ( $dbTo ['db_charset'], $linkTo );

	if ($pointFrom > 0) {
		/* 根据当前积分修改应用目标积分 */
		$sql = "UPDATE {$tableNameTo} SET {$dbTo['db_field_point']} = {$pointFrom} WHERE {$dbTo['db_field_username']}='{$username}' AND {$dbTo['db_field_email']}='{$email}' ";
		mysql_query ( $sql, $linkTo );
	} else {
		/* 获取目标应用的积分 */
		$sql = "SELECT {$dbTo['db_field_point']} point FROM {$tableNameTo} WHERE {$dbTo['db_field_username']}='{$username}' AND {$dbTo['db_field_email']}='{$email}' ";
		$resultTo = mysql_query ( $sql, $linkTo );
		while ( $row = mysql_fetch_array ( $resultTo ) ) {
			$pointTo = $row ['point'];
		}
		mysql_free_result ( $resultTo );

		/* 根据目标积分修改当前应用积分 */
		$sql = "UPDATE {$tableNameFrom} SET {$dbFrom['db_field_point']} = {$pointTo} WHERE {$dbFrom['db_field_username']}='{$username}' AND {$dbFrom['db_field_email']}='{$email}' ";
		mysql_query ( $sql, $linkTo );
	}

	/* 关闭mysql连接 */
	mysql_close ( $linkFrom );
	mysql_close ( $linkTo );

}

